package com.trust.walletcore.example;

import com.trust.walletcore.example.utils.SolanaTransactionSender;

import java.io.IOException;

import okhttp3.*;

public class SolanaFaucet {

   // private static final String FAUCET_URL = "https://api.devnet.solana.com";  // Devnet Solana Faucet URL

    public static void requestFaucet(String walletAddress) {
        OkHttpClient client = new OkHttpClient();

        // 请求体：以 POST 请求的方式发起请求
        String json = "{\n" +
                "  \"jsonrpc\": \"2.0\",\n" +
                "  \"id\": 1,\n" +
                "  \"method\": \"requestAirdrop\",\n" +
                "  \"params\": [\n" +
                "    \"" + walletAddress + "\",\n" +
                "    1000000000  // 请求 1 SOL (单位：lamports)\n" +
                "  ]\n" +
                "}";

        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);

        // 创建请求
        Request request = new Request.Builder()
                .url(SolanaTransactionSender.solanaRpcUrl)
                .post(body)
                .build();

        // 发送请求
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    System.out.println("Faucet request successful: " + response.body().string());
                } else {
                    System.err.println("Faucet request failed: " + response.body().string());
                }
            }
        });
    }
}
